package com.ydj.framework.module.city.service;

import com.ydj.framework.module.city.cache.DictCityCache;
import com.ydj.framework.module.city.dto.DictCityCityDto;
import com.ydj.framework.module.city.dto.DictCityDistrictDto;
import com.ydj.framework.module.city.dto.DictCityProvinceDto;
import com.ydj.framework.module.city.protocol.req.DictCityGetCityByProvinceIdReq;
import com.ydj.framework.module.city.protocol.req.DictCityGetDistrictListByCityIdReq;
import com.ydj.framework.module.city.protocol.req.DictCityGetProvinceListReq;
import com.ydj.framework.module.city.protocol.res.DictCityGetCityByProvinceIdRes;
import com.ydj.framework.module.city.protocol.res.DictCityGetDistrictListByCityIdRes;
import com.ydj.framework.module.city.protocol.res.DictCityGetProvinceListRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 所有项目通用的
 */
@Service
public class DictCityService {
    @Autowired
    private DictCityCache dictCityCache;

    /**
     * 获取所有省份
     */
    public DictCityGetProvinceListRes getProvinceList(DictCityGetProvinceListReq req) {
        DictCityProvinceDto dto = dictCityCache.getProvinceList();

        List<DictCityGetProvinceListRes.DictCityGetProvinceListItem> list = dto.getList().stream()
                .map(item-> new DictCityGetProvinceListRes.DictCityGetProvinceListItem()
                        .setId(item.getId())
                        .setName(item.getName())
                        .setHasChildren(item.getHasChildren()))
                .collect(Collectors.toList());

        return new DictCityGetProvinceListRes()
                .setList(list);
    }

    /**
     * 根据省份ID获取所有城市
     */
    public DictCityGetCityByProvinceIdRes getCityListByProvinceId(DictCityGetCityByProvinceIdReq req) {
        DictCityCityDto dto = dictCityCache.getCityList(req.getId());

        List<DictCityGetCityByProvinceIdRes.DictCityGetCityByProvinceIdItem> list = dto.getList().stream()
                .map(item-> new DictCityGetCityByProvinceIdRes.DictCityGetCityByProvinceIdItem()
                        .setId(item.getId())
                        .setName(item.getName())
                        .setHasChildren(item.getHasChildren()))
                .collect(Collectors.toList());

        return new DictCityGetCityByProvinceIdRes()
                .setList(list);
    }

    /**
     * 根据城市ID获取所有区
     */
    public DictCityGetDistrictListByCityIdRes getDistrictListByCityId(DictCityGetDistrictListByCityIdReq req) {
        DictCityDistrictDto dto = dictCityCache.getDistrictList(req.getId());

        List<DictCityGetDistrictListByCityIdRes.DictCityGetDistrictListByCityIdItem> list = dto.getList().stream()
                .map(item -> new DictCityGetDistrictListByCityIdRes.DictCityGetDistrictListByCityIdItem()
                        .setId(item.getId())
                        .setName(item.getName()))
                .collect(Collectors.toList());

        return new DictCityGetDistrictListByCityIdRes()
                .setList(list);
    }

}
